<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css" rel="stylesheet">
    <link href="/css/datatables.min.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <script src="/js/jquery-3.1.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/vue.js"></script>
</head>
<body style="background-color:whitesmoke">
<div id="user">
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>用户管理</h2>
            <ol class="breadcrumb">
                <li>
                    <strong>系统管理</strong>
                </li>
                <li class="active">
                    <strong>用户管理</strong>
                </li>
            </ol>
        </div>
    </div>

    <div class="wrapper wrapper-content animated fadeInRight">

        <div class="ibox-content m-b-sm border-bottom">
            <div class="row">
                <form role="form" class="form-horizontal">
                    <div class="form-group">

                        <label class=" col-sm-2 control-label">编号</label>
                        <div class=" col-sm-2">
                            <input type="text" class="form-control" v-model="userQuery.sysId" placeholder="请输入编号">
                        </div>

                        <label class=" col-sm-2 control-label">用户名</label>
                        <div class=" col-sm-2">
                            <input type="text" class="form-control" v-model="userQuery.sysName" placeholder="请输入用户名">
                        </div>

                        <div>
                            <button class="btn btn-primary btn-sm" type="button" @click="loadData()"><span
                                    class="glyphicon glyphicon-zoom-in"></span> 查询
                            </button>
                            <button class="btn btn-primary btn-sm" type="button" @click="add()"><span
                                    class="glyphicon glyphicon-plus"></span> 新增
                            </button>
                        </div>
                    </div>
                </form>

            </div>

        </div>
        <div class="row">
            <div class="col-lg-12">

                <div class="ibox float-e-margins">

                    <div class="ibox-content">

                        <table class="footable table table-stripped toggle-arrow-tiny" data-page-size="15">
                            <thead>
                            <tr>

                                <th>编号</th>
                                <th>用户名</th>
                                <th>密码</th>
                                <!--<th data-hide="phone">盐值</th>-->
                                <th>邮箱</th>
                                <th data-sort-ignore="true">操作</th>

                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="x in list">
                                <td>{{x.sysId}}</td>
                                <td>{{x.sysName}}</td>
                                <td>{{x.sysPwd}}</td>
                                <!--<td>{{x.salt}}</td>-->
                                <td>{{x.sysEmail}}</td>
                                <td>

                                    <button class="btn-primary btn btn-sm" @click="update(x)"><span
                                            class="glyphicon glyphicon-tag"></span> 编辑
                                    </button>
                                    <button class="btn-primary btn btn-sm" type="button" @click="del(x)"><span
                                            class="glyphicon glyphicon-trash"></span> 删除
                                    </button>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                        <div style="text-align: center;">
                            <button class="btn btn-success btn-sm" type="button" @click="indexPage()">首页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="prePages()">上一页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="nextPages()">下一页</button>
                            <button class="btn btn-success btn-sm" type="button" @click="endPage()">尾页</button>
                            跳转到<input type="text" style="width: 50px" v-model="selectPage" @keyup="valNum">页
                            <button class="btn btn-success btn-sm" type="button" @click="changePage()">跳转</button>
                            <select v-model="userQuery.row" @change="selectRow">
                                <option value="3">每页3条</option>
                                <option value="5">每页5条</option>
                                <option value="7">每页7条</option>
                            </select>
                            <br>
                            一共{{total}}条数据,一共{{totalPage}}页,当前第{{curPage}}页
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 模态框开始 -->
    <div id="one" class="modal fade" style="top:200px">
        <div class="modal-dialog">

            <div class="modal-content">
                <!--头部-->
                <div class="modal-header">
                    <h4 align="center">{{title}}</h4>

                </div>
                <!--主题内容-->
                <div class="modal-body">
                    <form role="form" class="form-horizontal">


                        <div class="form-group">
                            <label class=" col-sm-2 control-label">用户名</label>
                            <div class=" col-sm-9">
                                <input type="text" class="form-control" v-model="user.sysName" placeholder="请输入用户名"
                                       required="required"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class=" col-sm-2 control-label">密码</label>
                            <div class=" col-sm-9">
                                <input type="text" class="form-control" v-model="user.sysPwd" placeholder="请输入密码"
                                       required="required"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class=" col-sm-2 control-label">邮箱</label>
                            <div class=" col-sm-9">
                                <input type="email" id="email" class="form-control" v-model="user.sysEmail"
                                       placeholder="请输入邮箱(name@example.com)" required="required"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class=" col-sm-2 control-label">验证码</label>
                            <div class=" col-sm-9">
                                <input type="text" class="form-control" v-model="code.checkCode"
                                       placeholder="请输入验证码" required="required"/>
                            </div>
                        </div>

                    </form>
                </div>

                <!--底部-->
                <div class="modal-footer">
                    <button class="btn btn-primary btn-sm" type="button" @click="sendCode"><span
                            class="glyphicon glyphicon-envelope"></span>发送验证码
                    </button>
                    <button class="btn btn-primary btn-sm" type="button" @click="closeWin"><span
                            class="glyphicon glyphicon-remove"></span>关闭
                    </button>
                    <button class="btn btn-primary btn-sm" @click="saveUser"><span
                            class="glyphicon glyphicon-save"></span>保存
                    </button>
                </div>

            </div>
        </div>
    </div>

</div>
</body>

<script>

    new Vue({
        el: "#user",
        data: {
            list: [],
            total: 0,//总条数
            totalPage: 0,//总页数
            selectPage: null,//选择页数
            userQuery: { //查询对象
                page: 1,
                row: 3,
                sysId: "",
                sysName: "",
                sysEmail: ""
            },
            prePage: 0,//上一页
            nextPage: 0,//下一页.
            curPage: 0,//当前页
            user: {
                sysId: "",
                sysName: "",
                sysPwd: "",
                sysEmail: ""
            },
            code: {
                checkCode: ""
            },
            title: "用户修改"

        },
        methods: {
            loadData: function () {
                var self = this;
                $.ajax({
                    url: "/userReful/userList",//请求地址
                    type: "get",
                    data: self.userQuery,
                    dataType: "json",//返回的数据类型
                    success: function (data) {
                        console.log(data);
                        self.list = data.list;
                        self.total = data.total;
                        self.totalPage = data.totalPage;
                        self.prePage = data.prePage;
                        self.nextPage = data.nextPage;
                        self.curPage = data.curPage;
                    }

                })
            },
            prePages: function () {//上一页
                this.userQuery.page = this.prePage;
                //刷新
                this.loadData();

            },
            nextPages: function () {
                this.userQuery.page = this.nextPage;
                //刷新
                this.loadData();

            },
            indexPage: function () {//首页
                this.userQuery.page = 1
                //刷新
                this.loadData();

            },
            endPage: function () {//尾页
                this.userQuery.page = this.totalPage;
                //刷新
                this.loadData();

            },
            changePage: function () {//跳转
                if (this.selectPage > this.totalPage) {
                    this.userQuery.page = this.totalPage;
                } else {
                    this.userQuery.page = this.selectPage;
                }
                //刷新
                this.loadData();

            },
            selectRow: function () {//选择条数
                this.userQuery.page = 1
                //刷新
                this.loadData();

            },
            valNum: function () {
                //定义正则表达式对象
                var dd = /^[0-9]*$/
                //如果输入的汉字，就显示1
                if (dd.test(this.selectPage)) {
                    if (this.selectPage > this.totalPage) {
                        this.selectPage = this.totalPage;
                    }
                } else {
                    this.selectPage = null;
                }
            },
            sendCode: function () {//发送验证码
                var self = this;
                $.ajax({
                    url: "/email/sendCode",
                    type: "post",
                    contentType: "application/x-www-form-urlencoded",
                    data: {"email": self.user.sysEmail},
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        alert(data.info)
                    }
                })
            },
            update: function (x) {//打开修改模态框
                //数据回显
                this.user.sysId = x.sysId;
                this.user.sysName = x.sysName;
                this.user.sysPwd = x.sysPwd;
                this.user.sysEmail = x.sysEmail;
                this.title = "用户修改";
                $("#one").modal("show")
            },
            add: function () {//打开新增模态框
                $("#one").modal("show")
                this.title = "用户新增";
                //清空
                this.user.sysId = null;
                this.user.sysName = "";
                this.user.sysPwd = "";
                this.user.sysEmail = ""
            },
            del: function (x) {
                var is = confirm("你确定要删除吗？")
                var self = this;
                if (is) {
                    $.ajax({
                        url: "/userReful/delUser",
                        type: "post",
                        contentType: "application/x-www-form-urlencoded",
                        data: {"id": x.sysId},
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            alert(data.info);
                            if (data.info == "删除成功") {
                                //刷新
                                self.loadData();
                            }
                        }
                    })
                }
            },
            saveUser: function () {//保存
                var self = this;
                var email = document.getElementById("email").value;
                var regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
                if (self.user.sysPwd == "" || self.user.sysName == "") {
                    alert("请填写相关信息！");
                } else {
                    if (regex.test(email)) {
                        $.ajax({
                            url: "/email/checkCode",
                            type: "post",
                            contentType: "application/x-www-form-urlencoded",
                            data: {"code": self.code.checkCode},
                            dataType: "json",
                            success: function (data) {
                                if (data.info == "验证码错误") {
                                    alert(data.info);
                                } else {
                                    var url = "";
                                    if (self.title == "用户新增") {
                                        url = "/userReful/addUser";
                                        console.log(url)
                                    } else {
                                        url = "/userReful/updateUser";
                                        console.log(url)
                                    }
                                    console.log(url);
                                    $.ajax({
                                        url: url,
                                        type: "post",
                                        contentType: "application/x-www-form-urlencoded",
                                        data: self.user,
                                        dataType: "json",
                                        success: function (data) {
                                            console.log(data);
                                            alert(data.info);
                                            if (data.info == "保存成功") {
                                                //关闭模态框
                                                $("#one").modal("hide")
                                                //刷新
                                                self.loadData();
                                            }
                                        }

                                    })
                                }
                            }
                        })

                    } else {
                        alert("邮箱不正确");
                    }
                }
            },
            closeWin: function () {//关闭模态框
                $("#one").modal("hide")
            }


        },
        mounted() {
            //运行loadData函数
            this.loadData();
        }
    })


</script>
</html>
